package cn.szzsi.business.task;

import cn.szzsi.AConfig;
import cn.szzsi.model.BrandStore;
import cn.szzsi.model.Member;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.HttpKit;
import com.jfinal.plugin.cron4j.ITask;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.List;
import java.util.Scanner;

public class StoreLocationTask implements ITask {


    @Override
    public void stop() {

    }

    @Override
    public void run() {
        List<BrandStore> brandStores=BrandStore.dao.find("select * from sys_brand_store where  longitude is null and (city is not null and city !='')");
        for(BrandStore brandStore:brandStores){
            String url="https://api.map.baidu.com/geocoder/v2/?";
            try {
                String address=brandStore.getProvince()+brandStore.getCity()+brandStore.getAddress();
                address= URLEncoder.encode(address,"utf-8");
                String para="address="+address+"&output=json&ak="+ AConfig.p.get("BAIDU_KEY")+"&ret_coordtype=gcj02ll";
                String result= HttpKit.get(url+para);
                JSONObject jsonObject= JSON.parseObject(result);
                String longitude=jsonObject.getJSONObject("result").getJSONObject("location").get("lng").toString();
                String latitude=jsonObject.getJSONObject("result").getJSONObject("location").get("lat").toString();
                brandStore.setLongitude(new BigDecimal(longitude));
                brandStore.setLatitude(new BigDecimal(latitude));
                brandStore.update();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }

    }

    /*public static void main(String[] args) {
        String url="https://api.map.baidu.com/geocoder/v2/?";
        try {
            String address="长沙";
            address= URLEncoder.encode(address,"utf-8");
            String para="address="+address+"&output=json&ak="+ AConfig.p.get("BAIDU_KEY")+"&ret_coordtype=gcj02ll";
            String result= HttpKit.get(url+para);
            JSONObject jsonObject= JSON.parseObject(result);
            String longitude=jsonObject.getJSONObject("result").getJSONObject("location").get("lng").toString();
            String latitude=jsonObject.getJSONObject("result").getJSONObject("location").get("lat").toString();
            System.out.println("longitude---"+longitude);
            System.out.println("latitude---"+latitude);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }*/

    /*public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("输入身份证号码: ");
        String code = scan.nextLine().trim();
        scan.close();
        String reg = "^(.{6})(.{4})(.{2}).*$";
        String a = code.replaceAll(reg, "$1");
        String y = code.replaceAll(reg, "$2");
        String m = code.replaceAll(reg, "$3");
        System.out.println("所在地区代码：" + a);
        System.out.println("出生年月: " + y + ", " + m);
    }*/
}
